package me.zhyx.music.init;

import lombok.extern.slf4j.Slf4j;
import me.zhyx.music.dao.ProxyInterface;
import me.zhyx.music.model.Proxy_ip;
import me.zhyx.music.thread.producer.GatherThread;
import me.zhyx.music.utils.PoiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Auther: yssq
 * @Date: 2019/3/14 15:21
 * @Description:
 */
@Component
@Slf4j
public class InitSys {
    @Autowired
    ProxyInterface proxyInterface;
    @PostConstruct
    public void init() {
        importProxyIp();
        createThreadPool();

    }

    private void createThreadPool() {
        ExecutorService executorService = Executors.newFixedThreadPool(50);
        for(int i=0;i<100;i++){
            executorService.execute(new GatherThread());
        }
    }

    private void importProxyIp() {
        try {
            List<Proxy_ip> proxy_ips = PoiUtil.poiToObject("d:\\proxy1.xls");
            int i= proxyInterface.insertProxy(proxy_ips);
            log.info("insert success line {}",i);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("import proxy ip exception!");
        }
    }
}
